﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WadViewer.Configuration
{
	public static class ConnectionManager
	{
		public static IEnumerable<TableConnection> GetConnections()
		{
			// Get current configuration file.
			System.Configuration.Configuration config =
					ConfigurationManager.OpenExeConfiguration(
					ConfigurationUserLevel.None);

			// Get the section.
			AzureConnectionsSection section =
					config.GetSection("azureConnections") as AzureConnectionsSection;

			// If section not available 
			if (section == null)
				throw new InvalidOperationException("Failed to load section.");

			// return connections
			foreach (AzureConnectionConfigElement connection in section.AzureConnections)
			{
				yield return new TableConnection
				{
					Name = connection.Name,
					BaseAddress = connection.TableUrl,
					AccountName = connection.AccountName,
					Key = connection.Key
				};
			}
		}
	}
}
